<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
Yii::import('zii.widgets.jui.CJuiInputWidget');

class ListIcon extends CJuiInputWidget {

    /**
     * array of icons for the widgets
     *
     * attribute is associate in array with $key=>$value
     *
     * $key available are
     * value -> Text/value of the icon
     * url -> The URL link of the icon. default is set to '#'
     * icon -> The CSS class name of the jquery icon, array for primary and secondary icon
     * click -> Icon click event. 
     *
     * @var array
     */
    public $icons = array();

    public $container = 'div';
    
    private $baseUrl;
    
    public function init() {
    	if(!Yii::app()->request->isAjaxRequest)
    		parent::init();
    }
    public function run() {
        $this->baseUrl = Yii::getPathOfAlias('application.widgets.listicon');
        $this->htmlOptions['id'] = $this->getId();
        if(count($this->icons)>0) {
            foreach($this->icons as $iconid=>$attribute) {
                $attribute['htmlOptions']['id'] = $this->getId().'_'.$iconid;
                if(!isset($attribute['value']))
                    $attribute['value']='';
                if(isset($attribute['icon']) && is_array($attribute['icon'])){
                    $attribute['htmlOptions']['pIcon'] = $attribute['icon']['primary'];
                    $attribute['htmlOptions']['sIcon'] = $attribute['icon']['secondary'];
                }
                elseif(isset($attribute['icon'])) {
                    $attribute['htmlOptions']['pIcon'] = $attribute['icon'];
                }
                if(isset($attribute['needtext']))
                    $attribute['htmlOptions']['needText'] = $attribute['needtext'];
                $html .= CHtml::tag('a', $attribute['htmlOptions'], $attribute['value']);
            }
            $html = CHtml::tag($this->container,$this->htmlOptions,$html);
            $this->publishAssets();
            echo $html;
        }
    }
    private function publishAssets($js='') {
        $publishurl = Yii::app()->getAssetManager()->publish($this->baseUrl);
        $cs = Yii::app()->getClientScript();
        $ScriptPos = (Yii::app()->request->isAjaxRequest)?CClientScript::POS_END:CClientScript::POS_READY;
        $id = $this->htmlOptions['id'];
        $script = "$('div#$id').listicon()";
        $cs->registerScript(__CLASS__.$id,$script,$ScriptPos);
        if(!Yii::app()->request->isAjaxRequest)
      		$cs->registerScriptFile($publishurl.'/asset/ListIcon.js');
        //$cs->registerScriptFile($publishurl.'/asset/ListIcon.css');
    }
}
?>
